﻿using CsvHelper;
using Microsoft.AzureAD.XPrize.TwittOracle.FeatureExtraction;
using ml.Attributes;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using TweetSharp;

namespace Microsoft.AzureAD.XPrize.TwittOracle
{
    public class TwitterAccount
    {
        public string ScreenName { get; set; }
        private int followers;
        private int following;

        [Feature]
        public int Followers { 
            get
            {
               return followers;
            }
            set
            {
                if (value > 4000) // prevent skewed data
                    followers = 4000;
                else
                    followers = value;
            }
        }

        [Feature]
        public int Following
        {
            get
            {
                return following;
            }
            set
            {
                if (value > 4000) // prevent skewed data
                    following = 4000;
                else
                    following = value;
            }
        }

        public double PowerOfWord { get; set; }

        public double ProfileCompletionFactor { get; set; }

        [Label]
        public bool IsValid { get; set; }

        internal static TwitterAccount[] GetData()
        {
            List<TwitterAccount> data = new List<TwitterAccount>();

            var files = new Dictionary<string, bool>();
            // Format below: <data_filename, label>
            files.Add("accounts_valid.csv", true);
            files.Add("accounts_invalid.csv", false);

            foreach (var entry in files) {
                string root_path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string fileName = System.IO.Path.Combine(root_path, "Data", entry.Key);
                StreamReader infile = new StreamReader(fileName, true);
                CsvReader reader = new CsvReader(infile);
                int count = 0;

                string featureDataFile = System.IO.Path.Combine(root_path, "Data", "features.csv");
                StreamWriter outfile = new StreamWriter(featureDataFile, true);
                CsvWriter writer = new CsvWriter(outfile);
                while (reader.Read())
                {
                    var validAccount = FeatureExtractor.Get(reader.CurrentRecord);
                    validAccount.IsValid = entry.Value;
                    data.Add(validAccount);
                    writer.WriteField(validAccount.ScreenName);
                    writer.WriteField(validAccount.Followers);
                    writer.WriteField(validAccount.Following);
                    writer.WriteField(validAccount.IsValid);
                    writer.NextRecord();

                    count++;
                    if (count > 2000)
                        break;
                }

                outfile.Close();
                infile.Close();
            }

            return data.ToArray<TwitterAccount>();
        }

        internal static TwitterAccount GetSingle(TwitterUser user)
        {
            var account = new TwitterAccount();
            account.ScreenName = user.ScreenName;
            account.Followers = user.FollowersCount;
            account.Following = user.FriendsCount;

            return account;
        }
    }
}
